package com.hezuti.upload.action;

import java.io.File;
import com.hezuti.common.utils.ConfigUtil;
import com.hezuti.common.utils.FileUtil;
import com.hezuti.common.utils.ImageUtil;
import com.hezuti.sns.action.SnsAction;

public class ImageUploadAction extends SnsAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3665492246061372594L;

	private  File upload;
	
	private String uploadFileName;
	
	private String uploadContentType;
	
	private String preUrl="/";
	
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private int w,h;
	
	/**图片类别：头像：head,消息图片：msg*/
	private String type="msg";
	
	
	@Override
	public String execute() throws Exception {
		String path=ConfigUtil.getInstance().getString("userdata");
		String path2="data"+File.separator+"userdata"+File.separatorChar+String.valueOf(getUserInfo().getId()).hashCode()+File.separatorChar+type+File.separatorChar+"head"+uploadFileName.substring(uploadFileName.indexOf("."));
		File dest=new File(path+path2);
		File dest_180x180=new File(path+"data"+File.separator+"userdata"+File.separatorChar+String.valueOf(getUserInfo().getId()).hashCode()+File.separatorChar+type+File.separatorChar+"head_180x180"+uploadFileName.substring(uploadFileName.indexOf(".")));
		File dest_50x50=new File(path+"data"+File.separator+"userdata"+File.separatorChar+String.valueOf(getUserInfo().getId()).hashCode()+File.separatorChar+type+File.separatorChar+"head_50x50"+uploadFileName.substring(uploadFileName.indexOf(".")));
		File dest_100x100=new File(path+"data"+File.separator+"userdata"+File.separatorChar+String.valueOf(getUserInfo().getId()).hashCode()+File.separatorChar+type+File.separatorChar+"head_100x100"+uploadFileName.substring(uploadFileName.indexOf(".")));
		if(!dest.getParentFile().exists())dest.getParentFile().mkdirs();
		int[] w_h=ImageUtil.getImageWidthAndHeight(upload);
		if(x1==x2||y1==y2){
			FileUtil.cp(upload, dest);
			getResponse().getWriter().println("{'url':'"+preUrl+path2.replace(File.separator, "/")+"','h':"+w_h[1]+",'w':"+w_h[0]+"}");
			return null;
		}
		if(dest.exists())dest.delete();
		if(dest_100x100.exists())dest_100x100.delete();
		if(dest_180x180.exists())dest_180x180.delete();
		if(dest_50x50.exists())dest_50x50.delete();
		//ImageUtil.cutImage(upload.getAbsolutePath(), dest.getAbsolutePath(),Math.round(x1*(w_h[0]/w)),Math.round(y1*(w_h[1]*1.0f/h)),Math.round(x2*(w_h[0]*1.0f/w)),Math.round(y2*(w_h[1]*1.0f/h)));
		ImageUtil.cutImage(upload.getAbsolutePath(), dest.getAbsolutePath(),x1,y1,x2,y2);
		ImageUtil.scaleImage(dest.getAbsolutePath(), dest_180x180.getAbsolutePath(), 180d, 180d);
		ImageUtil.scaleImage(dest.getAbsolutePath(), dest_100x100.getAbsolutePath(), 100d, 100d);
		ImageUtil.scaleImage(dest.getAbsolutePath(), dest_50x50.getAbsolutePath(), 50d, 50d);
		
		userService.doModifyHeadPic(path2, getUserInfo().getId());
		getUserInfo().setHeadPicName(path2);
		return SUCCESS;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getPreUrl() {
		return preUrl;
	}

	public void setPreUrl(String preUrl) {
		this.preUrl = preUrl;
	}

	public int getX1() {
		return x1;
	}

	public void setX1(int x1) {
		this.x1 = x1;
	}

	public int getY1() {
		return y1;
	}

	public void setY1(int y1) {
		this.y1 = y1;
	}

	public int getX2() {
		return x2;
	}

	public void setX2(int x2) {
		this.x2 = x2;
	}

	public int getY2() {
		return y2;
	}

	public void setY2(int y2) {
		this.y2 = y2;
	}

	public int getW() {
		return w;
	}

	public void setW(int w) {
		this.w = w;
	}

	public int getH() {
		return h;
	}

	public void setH(int h) {
		this.h = h;
	}

	
	
}
